package com.ruoyix.novel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.novel.domain.bo.NovelVolumeBo;
import com.ruoyix.novel.domain.vo.NovelVolumeVo;
import com.ruoyix.novel.domain.NovelVolume;
import com.ruoyix.novel.mapper.NovelVolumeMapper;
import com.ruoyix.novel.service.INovelVolumeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 分卷设计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-05
 */
@RequiredArgsConstructor
@Service
public class NovelVolumeServiceImpl implements INovelVolumeService {

    private final NovelVolumeMapper baseMapper;

    /**
     * 查询分卷设计
     */
    @Override
    public NovelVolumeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分卷设计列表
     */
    @Override
    public TableDataInfo<NovelVolumeVo> queryPageList(NovelVolumeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NovelVolume> lqw = buildQueryWrapper(bo);
        Page<NovelVolumeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询分卷设计列表
     */
    @Override
    public List<NovelVolumeVo> queryList(NovelVolumeBo bo) {
        LambdaQueryWrapper<NovelVolume> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NovelVolume> buildQueryWrapper(NovelVolumeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelVolume> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProjectId() != null, NovelVolume::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), NovelVolume::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), NovelVolume::getDescription, bo.getDescription());
        lqw.eq(bo.getOrderIndex() != null, NovelVolume::getOrderIndex, bo.getOrderIndex());
        return lqw;
    }

    /**
     * 新增分卷设计
     */
    @Override
    public Boolean insertByBo(NovelVolumeBo bo) {
        NovelVolume add = BeanUtil.toBean(bo, NovelVolume.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分卷设计
     */
    @Override
    public Boolean updateByBo(NovelVolumeBo bo) {
        NovelVolume update = BeanUtil.toBean(bo, NovelVolume.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelVolume entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除分卷设计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
